package me.minecraft.plugin.itemsave.command;

import me.minecraft.plugin.itemsave.manager.ItemManager;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class SaveCommand extends SubCommand {

    @Override
    public void onCommand(Player player, String[] args) {
        if (args.length < 1) {
            player.sendMessage("[§6ItemSave§f] §c缺少物品名称");
            return;
        }

        String name = args[0];
        ItemStack item = player.getInventory().getItemInMainHand();
        if (item.getType() == Material.AIR) {
            player.sendMessage("[§6ItemSave§f] 请手持物品");
            return;
        }

        if (ItemManager.INSTANCE.exists(name)) {
            player.sendMessage("[§6ItemSave§f] 物品 " + name + " 已存在");
            return;
        }

        if (ItemManager.INSTANCE.saveItem(name, item.clone())) {
            player.sendMessage("[§6ItemSave§f] 保存物品 " + name + " 成功");
        } else {
            player.sendMessage("[§6ItemSave§f] 保存物品 " + name + " 失败");
        }
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, String[] args) {
        return List.of("<name>");
    }

}
